<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing:  ControllerAction
 *
 * @category   	Gnn Cms
 * @package    	Gnn ControllerAction
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 Jun 8, 2013 || 10:11:47 AM
 */
class Zendvn_Controller_Action extends Zend_Controller_Action{
	public $gcLoadLayout = true;
	public $gcClient_id;
	public $gcTemplate;
	public $gcPage;
	public $gcComponent;
	public $gcRequestParams;
	public $gcComponentHelper;
	public $gcComponentContent;
	
	public function init(){
		$this->gcRequestParams 			= $this->_request->getParams();
		$this->view->gcRequestParams 	= $this->gcRequestParams;
		$info = new Zendvn_System_Info();
		$lang 							= (isset($this->gcRequestParams['gclang'])) ?  $this->gcRequestParams['gclang']
												:  GLOBAL_LANGGUAGE;
		//luu language vao session
		$info->setLanguage($lang);
		
		$locale 						= new Zend_Locale($lang);
		Zend_Registry::set(REGISTRY_KEY_LOCALE, $locale);
		
		
		$this->initPage();
		$this->initTemplate();
		$this->initComponent();
		$this->initModule();
		$this->appRun();
	}
	// lay thong tin ve component trong database
	// neu type dang http://localhost/gcms/default/index/index/componentname/ccccc/control/ccc/activity/aaa thi khong vao db lay
	
	protected function initPage(){
		// detect area: frontend (1) or backend (0)
		$this->gcClient_id  	= ($this->view->gcRequestParams['action'] == 'index') ? 1 : 0;
		/*
		 * neu nhu khong cung cap tham so gCpace tren Url thi
		 */
		if(empty($this->gcRequestParams['gcPage'])){
			// directly enter url
// 			$basePath  = ($this->gcClient_id == 1) ? MODULES_PATH_FRONT : MODULES_PATH_BACK;
			$this->gcPage 							= new stdClass();
			$this->gcPage->component_control 		= $this->gcRequestParams['control'];
			$this->gcPage->component_action 		= $this->gcRequestParams['activity'];
			$this->gcPage->component_id 			= 0;  // danh dau id = 0: ajax-activity, thay vì cung cấp component id, thì cung cấp tên
			$this->gcPage->component_name			= $this->gcRequestParams['component']; 
			$this->gcPage->client_id 				= $this->gcClient_id;
			$this->gcPage->template_style_id 		= 0;
		}else{
			$id   			 		= ($this->gcRequestParams['gcPage'] >0) 
										? $this->gcRequestParams['gcPage'] 
										: 1;
			
			$this->gcPage 	 		= new Zendvn_Page($id, $this->gcClient_id);
			$this->gcPage		    ->loadHeadInfo($this->view);
		}
	}
	protected function initComponent(){
		$this->gcComponent 				= new Zendvn_Component_Component($this->gcPage);
// 		$this->gcComponent 				= new Zendvn_Component_Component($this->gcPage->component_id);
		$this->gcComponentHelper		= new Zendvn_Component_Helper($this->gcComponent, $this->gcRequestParams,$this->gcPage);
		
// 		init language
		$tran 							= new Zendvn_Translate($this);
		
		$controllerPath 				= $this->gcComponentHelper->getComponentPath().DS.$this->gcComponentHelper->gcControllerName.'.php';
		$controllerClassName 			= $this->gcComponentHelper->getControllerClassName();
		$activityMethodName				= $this->gcComponentHelper->getActivityMethodName();
	
		ob_start();
		require_once $controllerPath;
		$ctrl  							= new $controllerClassName($this);
		$this->gcComponentContent		= ob_get_contents();
		ob_clean();
	}
	protected function initTemplate(){
		if($this->gcPage->template_style_id != 0) $this->gcTemplate       = new Zendvn_Template($this->gcClient_id, $this->gcPage->template_style_id, $this->view,$this->gcLoadLayout);
	}
	protected function initModule(){
		if($this->gcLoadLayout == true) $moduleHelper   		= new Zendvn_Modules_Helper($this->gcTemplate, $this->gcPage);
	}
	
	protected function appRun(){
		if($this->gcLoadLayout == true){
			$this->appendComponentContent();
			echo $this->gcTemplate->content;
		}
		else echo $this->gcComponentContent;
	}
	protected function appendComponentContent(){
		$this->gcTemplate->content = str_replace('{include {"type":"component"} include}', $this->gcComponentContent, $this->gcTemplate->content);
	}
}